package com.itlong.cloud.controller.parking;

import com.fasterxml.jackson.core.type.TypeReference;
import com.itlong.cloud.POJO.DTO.common.NettyServiceDTO;
import com.itlong.cloud.POJO.DTO.common.ParkingMessageDTO;
import com.itlong.cloud.POJO.DTO.common.ResultMessageDTO;
import com.itlong.cloud.POJO.DTO.property.CarIssueDTO;
import com.itlong.cloud.POJO.DTO.property.PropertyCarOpenMonthCardDTO;
import com.itlong.cloud.POJO.DTO.property.PropertyCarRenewMonthCardDTO;
import com.itlong.cloud.POJO.VO.MessageVO;
import com.itlong.cloud.abstracts.BaseController;
import com.itlong.cloud.annotation.AuthAccessRequired;
import com.itlong.cloud.annotation.LoggerInfo;
import com.itlong.cloud.common.network.tcp.netty.INettyServiceClient;
import com.itlong.cloud.constants.LogDescConstant;
import com.itlong.cloud.enumerate.ecode.BaseErrorCodeEnum;
import com.itlong.cloud.enumerate.ecode.PropertyErrorCodeEnum;
import com.itlong.cloud.project.property.IParkCarHttpServiceClient;
import com.itlong.cloud.project.property.IPropertyCarMonthCardServiceClient;
import com.itlong.cloud.service.RedisService;
import com.itlong.cloud.thrown.DataAccessException;
import com.itlong.cloud.utils.network.http.HttpProtocolUtil;
import com.itlong.cloud.utils.node.JsonUtil;
import com.itlong.cloud.utils.words.NumberHandlerUtil;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;

/**
 * <desc>
 *      物业管理-车辆的月卡管理
 * </desc>
 *
 * @createDate 2018/11/05
 */
@RestController
@RequestMapping("/parking/car")
public class PropertyCarMonthCardController extends BaseController {
    private static Logger LOG = LoggerFactory.getLogger(PropertyCarMonthCardController.class);

    @Autowired
    private IPropertyCarMonthCardServiceClient iPropertyCarMonthCardServiceClient;

    @Autowired
    private IParkCarHttpServiceClient iParkCarHttpServiceClient;

    @Autowired
    private INettyServiceClient iNettyServiceClient;

    @Autowired
    RedisService redisService;
    /**
     * <desc>
     *      车辆开通或修改月卡
     * </desc>
     *
     * @param propertyCarOpenMonthCardDTO 月卡开通、修改DTO
     * @return 标准格式化响应结果
     * @author wangzhi
     * @createDate 2018/11/05
     */
    @ResponseBody
    @PostMapping("openMonthCard")
    @LoggerInfo(operateDesc = LogDescConstant.CAR_OPEN_MONTH_CARD)
    @AuthAccessRequired
    public Object openMonthCard(PropertyCarOpenMonthCardDTO propertyCarOpenMonthCardDTO){
        try {
            HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
            propertyCarOpenMonthCardDTO = HttpProtocolUtil.parseRequestParamToDTO(PropertyCarOpenMonthCardDTO.class, request);
            if (propertyCarOpenMonthCardDTO == null || StringUtils.isBlank(propertyCarOpenMonthCardDTO.getCarId()) || StringUtils.isBlank(propertyCarOpenMonthCardDTO.getMonthCardId())
                    || StringUtils.isBlank(propertyCarOpenMonthCardDTO.getOwnerPhone())) {
                return new MessageVO(BaseErrorCodeEnum.NOT_PARAM.getErrorCode());
            }
            iPropertyCarMonthCardServiceClient.openMonthCard(propertyCarOpenMonthCardDTO);
            return new MessageVO(propertyCarOpenMonthCardDTO.getCarId(),BaseErrorCodeEnum.SUCCESS.getErrorCode());
        } catch (Exception e) {
            throw new DataAccessException("【物业-车辆管理】月卡开通或修改失败",e);
        }
    }

    /**
     * <desc>
     *      车辆月卡续费
     * </desc>
     *
     * @param propertyCarRenewMonthCardDTO 月卡续费DTO
     * @return 标准格式化响应结果
     * @author wangzhi
     * @createDate 2018/11/05
     */
    @ResponseBody
    @PostMapping("renewMonthCard")
    @LoggerInfo(operateDesc = LogDescConstant.CAR_RENEW_MONTH_CARD)
    @AuthAccessRequired
    public Object renewMonthCard(PropertyCarRenewMonthCardDTO propertyCarRenewMonthCardDTO){
        try {
            HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
            propertyCarRenewMonthCardDTO = HttpProtocolUtil.parseRequestParamToDTO(PropertyCarRenewMonthCardDTO.class, request);
            if (propertyCarRenewMonthCardDTO == null || StringUtils.isBlank(propertyCarRenewMonthCardDTO.getCarId()) || StringUtils.isBlank(propertyCarRenewMonthCardDTO.getMonthCardId())
                    || StringUtils.isBlank(propertyCarRenewMonthCardDTO.getPaidAmount())) {
                return new MessageVO(BaseErrorCodeEnum.NOT_PARAM.getErrorCode());
            }
            if (StringUtils.isBlank(propertyCarRenewMonthCardDTO.getProjectId())) {
                return new MessageVO(PropertyErrorCodeEnum.PUB_PROJECT_ID_EMPTY_ERR.getErrorCode());
            }
            if (!NumberHandlerUtil.isInRange(propertyCarRenewMonthCardDTO.getPaymentType(), 1, 7)) {
                return new MessageVO(PropertyErrorCodeEnum.PROPERTY_PAYMENT_TYPE_INVALID_ERR.getErrorCode());
            }
            if (!NumberHandlerUtil.isInRange(propertyCarRenewMonthCardDTO.getMonth(), 1, 24)) {
                return new MessageVO(PropertyErrorCodeEnum.PROPERTY_RENEW_MONTH_ERR.getErrorCode());
            }
            CarIssueDTO carIssueDTO = iPropertyCarMonthCardServiceClient.renewMonthCard(propertyCarRenewMonthCardDTO,"1");
            try {
                NettyServiceDTO nettyServiceDTO = iParkCarHttpServiceClient.carIssue(carIssueDTO);
                iNettyServiceClient.synOrderByParking(nettyServiceDTO.getClientKey(), nettyServiceDTO.getContent(),
                        nettyServiceDTO.getParkingBusinessOrderEnum(), nettyServiceDTO.getMessageId());

                int count = 0;
                while (count < 10) {
                    Thread.sleep(500);
                    ParkingMessageDTO parkingMessageDTO  = (ParkingMessageDTO)redisService.getObject(nettyServiceDTO.getMessageId(),ParkingMessageDTO.class);
                    //LOG.info("【月卡续费messageId】"+nettyServiceDTO.getMessageId());
                    if (parkingMessageDTO != null) {
                        ResultMessageDTO resultMessageDTO = JsonUtil.decode(parkingMessageDTO.getContent(), new TypeReference<ResultMessageDTO>() {});
                        //LOG.info("【月卡续费数据】"+JsonUtil.toJSON(resultMessageDTO));
                        if (resultMessageDTO.getCode() != null && resultMessageDTO.getCode().equals(0)) {
                            iPropertyCarMonthCardServiceClient.renewMonthCard(propertyCarRenewMonthCardDTO,"0");
                            return new MessageVO(propertyCarRenewMonthCardDTO.getCarId(),BaseErrorCodeEnum.SUCCESS.getErrorCode());
                        }
                    }
                    count++;
                }
            } catch (Exception e) {
                return new MessageVO(PropertyErrorCodeEnum.PROPERTY_RENEW_MONTH_CARD_ERR.getErrorCode());
            }
            return new MessageVO(PropertyErrorCodeEnum.PROPERTY_RENEW_MONTH_CARD_ERR.getErrorCode());
        } catch (Exception e) {
            throw new DataAccessException("【物业-车辆管理】月卡续费失败",e);
        }
    }

}
